<?php
namespace app\common\business;

use app\common\model\mysql\SpecsValue as SpecsModel;

class SpecsValue
{
    public $model = null;

    public function __construct()
    {
        $this->model = new SpecsModel();
    }

    public function add($data)
    {
        if (empty($data) || !is_array($data)) {
            throw new \think\Exception('数据异常');
        }

        $data['status'] = SpecsModel::STATUS_NORMAL;

        $name = $data['name'];
        $res = $this->model->findSpecsValueByname($name);
        if ($res) {
            throw new \think\Exception('名称已存在, 请重新添加');
        }
        try {
            $this->model->save($data);
        } catch (\Exception $e) {
            throw new \think\Exception('数据库内部异常');
        }
        return $this->model->id;
    }

    public function getBySpecsId($specsid)
    {
        try {
            $result = $this->model->getNormalBySpecsId($specsid, "id, name");
        } catch (\Exception $e) {
            return [];
        }

        return $result->toArray();
    }

    public function delSpecsValueById($id)
    {
        if (!$id || !is_numeric($id)) {
            throw new \think\Exception('数据异常');
        }
        return $this->model->delSpecsValueById($id);
    }
}